package servlet;

import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import dbOperator.AdvancedOperation;


@WebServlet("/UpdatePost")
public class UpdatePost extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		Cookie[] cookies = request.getCookies();
		int uid = 0;
		for(Cookie c : cookies) {
			if(c.getName().equals("LoginInfo")) {
				String currentCookie = c.getValue();
				try {
					uid = AdvancedOperation.getUidbyCookie(currentCookie);
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
		String title = request.getParameter("title");
		String content = request.getParameter("content");
		try {
			Connection con = dbOperator.BasicOperation.getConnection();
			dbOperator.AdvancedOperation.setAnnouncement(con, uid, title, content);
			con.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		}
	}
}
